package com.bkitmobile.kesu.android.GUI;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.DatePicker.OnDateChangedListener;

import com.appenginefan.toolkit.common.ClientSocketSubstitute;
import com.bkitmobile.kesu.android.ClassInfo.RegisterInfo;
import com.bkitmobile.kesu.android.connectServer.Request;
import com.bkitmobile.kesu.android.connectServer.ServiceCallback;
import com.bkitmobile.kesu.android.connectServer.staticClass;

public class Register extends Activity implements OnClickListener, OnKeyListener{
	
	private Button btnOK, btnCancel;
	private EditText txtID, txtPass, txtPassConfirm, txtFName, txtLName, txtEmail;
	private RadioButton rbMale;
	private RegisterInfo ri = new RegisterInfo();
	private DatePicker dpBdate;
	private Handler handler = new Handler();
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		init();
	}
	
	public RegisterInfo getRI(){
		return ri;
	}
	
	private void init(){
		setContentView(R.layout.register);
		btnOK = (Button)findViewById(R.id.register_btnOK);
		btnCancel = (Button)findViewById(R.id.register_btnCancel);
		btnOK.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		txtID = (EditText) findViewById(R.id.register_id);
		txtPass = (EditText) findViewById(R.id.register_pass);
		txtPassConfirm = (EditText) findViewById(R.id.register_confirmPass);
		txtFName = (EditText) findViewById(R.id.register_fname);
		txtLName = (EditText) findViewById(R.id.register_lname);
		txtEmail = (EditText) findViewById(R.id.register_email);
		dpBdate = (DatePicker) findViewById(R.id.register_bdate);
		
		
		rbMale = (RadioButton) findViewById(R.id.register_sex_male);
		
		btnOK.setClickable(false);
		txtID.setOnKeyListener(this);
		txtPass.setOnKeyListener(this);
		txtPassConfirm.setOnKeyListener(this);
		txtEmail.setOnKeyListener(this);
		final Date now = new Date();
		dpBdate.init(now.getYear() + 1900, now.getMonth(), now.getDate(), new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				if(year >= now.getYear() + 1900){
					if(monthOfYear >= now.getMonth()){
						if(dayOfMonth >= now.getDate())
							dpBdate.init(now.getYear()+1900, now.getMonth(), now.getDate(), this);
						else
							dpBdate.init(now.getYear()+1900, now.getMonth(), dayOfMonth, this);
					}
					else
						dpBdate.init(now.getYear()+1900, monthOfYear, dayOfMonth, this);
				}
				else if(year < now.getYear() + 1800)
					dpBdate.init(now.getYear()+1800, monthOfYear, dayOfMonth, this);
				
				
			}
		});
		dpBdate.setSoundEffectsEnabled(true);
		
		
	}

	public void onClick(View v) {
		if(btnOK == v){
			
			//TODO: check info
			if(!(txtPass.getText().toString().equals(txtPassConfirm.getText().toString()))){
				Toast.makeText(this, "Confirm password is not true", Toast.LENGTH_SHORT);
				return;
			}
			//showDialog(0);	
			ri.setId(txtID.getText().toString());
			ri.setPass(txtPass.getText().toString());
			ri.setfName(txtFName.getText().toString());
			ri.setlName(txtLName.getText().toString());
			ri.setEmail(txtEmail.getText().toString());
			
			int date = dpBdate.getDayOfMonth();
			int month = dpBdate.getMonth();
			int year = dpBdate.getYear();
			
			
				
			ri.setDate(date, month, year);
			Log.d("register", "BDate " + ri.getBDate());
			
			if(rbMale.isChecked())
				ri.setSex("male");
			else
				ri.setSex("female");
			request();
		} else finish();
		
	}

	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Boolean check = txtID.getText().toString().equals("") ||  
						txtPass.getText().toString().equals("") ||
						txtPassConfirm.getText().toString().equals("");
		
		//TODO: more check
		if(!check)
			btnOK.setClickable(true);
		return false;
	}
	private void request()
	{
		
		String []identities = {"ID","username","password","firstname","lastname","sex","birthday","email"};
		String []atributies = {Request.REGISTER, ri.getId(), ri.getPass(), ri.getfName(), ri.getfName()
				,ri.getSex(),ri.getBDate(), ri.getEmail()};

	         //nameValuePairs.add(new BasicNameValuePair("now", dateFormat.format(new Date())));
	     Request.sendAndRecieveData(identities, atributies, true,this,new ServiceCallback<String>() {
			
			@Override
			public void onSuccess(final String result) {
				// TODO Auto-generated method stub
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						if (result.startsWith("succ"))
						{
							Toast.makeText(Register.this, getString(R.string.register_success), Toast.LENGTH_SHORT).show();
							finish();
						} else {
							Toast.makeText(Register.this, getString(R.string.register_already_used), Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
			
			@Override
			public void onFailure(Exception exception) {
				// TODO Auto-generated method stub
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						StaticDialogs.okButtonDialog(Register.this
								, Register.this.getString(R.string.disconnect));
					}
				});
			}
		});
	}

}